/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DLChangeSkin.java
 *
 * Created on Sep 2, 2010, 12:07:37 AM
 */
package Gui;

import BusinessLogic.BackGroundUtility;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author SONY
 */
public class DLChangeSkin extends javax.swing.JDialog implements MouseListener {

    private Home home;
    private File[] listImage;
    private ImagePanel[] pnSkin;
    private String path = "";
    private int index = -1;

    /** Creates new form DLChangeSkin */
    public DLChangeSkin(Home home, boolean modal) {
        super(home, modal);
        this.home = home;

        initComponents();
        //set background
        ImagePanel iPanel = new ImagePanel(BackGroundUtility.getImage());
        getContentPane().add(iPanel);
        //setlocation for application
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        Dimension dimApp = getSize();
        int pointX = (dim.width - dimApp.width) / 2;
        int pointY = (dim.height - dimApp.height) / 2 - 10;
        setLocation(pointX, pointY);

        //load listSkin
        loadSkin();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnBackground = new javax.swing.JPanel();
        pnContent = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btnSelect = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        pnListSkin = new javax.swing.JPanel(new FlowLayout(FlowLayout.LEFT, 20, 10));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pnBackground.setOpaque(false);

        pnContent.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setLayout(null);

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });
        jPanel2.add(btnSelect);
        btnSelect.setBounds(390, 10, 70, 23);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);
        btnCancel.setBounds(470, 10, 70, 23);

        javax.swing.GroupLayout pnContentLayout = new javax.swing.GroupLayout(pnContent);
        pnContent.setLayout(pnContentLayout);
        pnContentLayout.setHorizontalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnListSkin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnContentLayout.setVerticalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnListSkin, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnBackgroundLayout = new javax.swing.GroupLayout(pnBackground);
        pnBackground.setLayout(pnBackgroundLayout);
        pnBackgroundLayout.setHorizontalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnBackgroundLayout.setVerticalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(pnBackground, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadSkin() {
        File file = new File("image/background");
        listImage = file.listFiles();
        int numberImg = listImage.length;
        pnSkin = new ImagePanel[numberImg];

        for (int i = 0; i < numberImg; i++) {

            pnSkin[i] = new ImagePanel(listImage[i].getPath(), 110, 90);
            pnSkin[i].setPreferredSize(new Dimension(110, 90));
            pnSkin[i].addMouseListener(this);

            pnListSkin.add(pnSkin[i]);
        }
    }
    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        //gettext from textfiels
        if (path.equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose a skin!!!", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            BackGroundUtility.setImage(path);
            home.setBackground(path);

            dispose();
        }

}//GEN-LAST:event_btnSelectActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSelect;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel pnBackground;
    private javax.swing.JPanel pnContent;
    private javax.swing.JPanel pnListSkin;
    // End of variables declaration//GEN-END:variables

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        Object obj = e.getSource();
        for (int i = 0; i < listImage.length; i++) {
            if (obj.equals(pnSkin[i])) {
                pnSkin[i].setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLUE, 2));
                path = listImage[i].getPath();
                index = i;
            }else{
                pnSkin[i].setBorder(null);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        Object obj = e.getSource();
        for (int i = 0; i < listImage.length; i++) {
            if (obj.equals(pnSkin[i]) && i != index) {
                pnSkin[i].setBorder(javax.swing.BorderFactory.createLineBorder(Color.lightGray, 2));
            }
        }
    }

    public void mouseExited(MouseEvent e) {
        for (int i = 0; i < listImage.length; i++) {
            if (index != i) {
                pnSkin[i].setBorder(null);
            }
        }
    }
}
